package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoService roomInfoService;

    @Override
    public void customSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo){

        //判断是否为更新
        boolean is = apartmentSubmitVo.getId() != null;
        //进行公寓信息保存或更新
        saveOrUpdate(apartmentSubmitVo);
        //如果更新，提前删除中间表格的数据
        if(is){
            //删除图片列表
            LambdaQueryWrapper<GraphInfo> LambdaQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            LambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoService.remove(LambdaQueryWrapper);

            //删除配套表
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);

            //删除标签列表
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelLambdaQueryWrapper);

            //删除杂费表
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);

        }
        //进行数据插入保存
        //1.插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>();
            for(GraphVo graphVo:graphVoList){

                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoArrayList.add(graphInfo);

            }
            graphInfoService.saveBatch(graphInfoArrayList);
        }

        //插入配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();

        if(!CollectionUtils.isEmpty(facilityInfoIdList)){
            ArrayList<ApartmentFacility> facilityArrayList = new ArrayList<>(facilityInfoIdList.size());
            for (Long facilityId : facilityInfoIdList) {
                ApartmentFacility facility = ApartmentFacility.builder().facilityId(facilityId).apartmentId(apartmentSubmitVo.getId()).build();
                facilityArrayList.add(facility);
            }
            apartmentFacilityService.saveBatch(facilityArrayList);
        }

        //公寓标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
            for(Long labelId : labelIds){
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentSubmitVo.getId()).build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        //公寓杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
            for(Long feeValueId : feeValueIds){
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(apartmentSubmitVo.getId()).build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
    }

    @Override
    public void queryPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo){
        apartmentInfoMapper.queryPage(page,queryVo);
    }


    @Override
    public ApartmentDetailVo queryDetail(Long id) {
        //1.id查询公寓信息详情
        ApartmentInfo apartmentInfo = getById(id);
        if(apartmentInfo == null){
            return null;
        }
        //2. 公寓id查询配套详情 (推荐直接调用配套的mapper,少了一层service的方法)
        //正常: 调用对方的service (事务的传播行为)
        //不正常: 1. service 只有一行 和 对应一个数据库动作 我们的service直接mapper依然能mapper加到当前事务
        //       2. 省一层代码结果  service xx -> mapper xx
        //        3. 有意避免循环注入的问题
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryByApartmentId(id);
        //3.公寓id查询图片集合
        List<GraphVo> graphVoList = graphInfoMapper.queryByItemId(id,ItemType.APARTMENT);
        //4.根据公寓id查询标签集合
        List<LabelInfo> labelInfoList = labelInfoMapper.queryByApartmentId(id);
        //5.公寓id查询杂费和杂费名集合
        List<FeeValueVo> feeValueVoList = feeValueMapper.queryByApartmentId(id);

        //6.合并到ApartmentDetailVo对象，返回
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        //将原对象的属性付给对象的相同属性，属性值迁移
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        return apartmentDetailVo;
    }
    @Override
    public void customRemoveById(Long id) {
        //查询是否公寓对应的房间
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(lambdaQueryWrapper);
        if (count > 0){
            //证明有房间  有重要子集 给与前端207
            //抛出异常,不会正常返回结果(业务正确), 使用全局异常处理器进行捕捉,返回对应的错误码给前端  DELETE_ERROR(207, "请先删除子集"),
            //自定义异常
            throw  new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //1.删除公寓
        removeById(id);
        //2.删除公寓相关的配套
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        //3.删除公寓相关的标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        //4.删除公寓相关的图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //5.删除公寓相关的杂费
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

    }
}




